<?php

/**
 * 
 * Class for manipulating with users and their data
 * @author sbojchuk
 *
 */
class User {
	
	private $db;
	
	public function __construct() {
		$this->db = Db::getInstance ();
	}
	
	public function addUser($user, $password, $email) {
		$password = md5 ( $password );
		$stmt = $this->db->prepare ( "INSERT INTO users 
		(login,password,email,created)
		VALUES
		(?,?,?,?);
		" );
		
		$stmt->bindParam ( 1, $user, PDO::PARAM_STR );
		$stmt->bindParam ( 2, $password, PDO::PARAM_STR );
		$stmt->bindParam ( 3, $email, PDO::PARAM_STR );
		$stmt->bindParam ( 4, date ( 'Y:n:d H:i:s' ), PDO::PARAM_STR );
		$stmt->execute ();
	}
	
	//check if that user login exists in DB
	public function userExists($user) {
		$stmt = $this->db->prepare ( "SELECT id FROM users WHERE login = ?" );
		$stmt->bindParam ( 1, $user, PDO::PARAM_STR );
		$stmt->execute ();
		if ($stmt->rowCount () !== 0) {
			return true;
		} else {
			return false;
		}
	}
	
	
	function getUserInfo($user_id) {
		$stmt = $this->db->prepare("SELECT *,-password FROM users WHERE id = ? LIMIT 1");
		$stmt->bindParam(1,$user_id,PDO::PARAM_INT);
		$stmt->execute();
		
		return $stmt->fetch(PDO::FETCH_ASSOC);
	}

}